package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.util.ShowToast;
import com.eightbears.beboy.base.view.PayPsdInputView;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;
import com.pnikosis.materialishprogress.ProgressWheel;


public class PasswordPayPopup extends BasePopupWindow {

    private onPasswordPayPopupClickListener itemClickListener = null;
    private OnClickSurePayListener onClickSurePayListener = null;
    private ProgressWheel mProgress_wheel;
    private TextView mTv_sure;

    public void setOnItemClickListener(onPasswordPayPopupClickListener listener) {
        this.itemClickListener = listener;
    }

    public void setOnClickSurePayListener(OnClickSurePayListener listener) {
        this.onClickSurePayListener = listener;
    }

    public interface onPasswordPayPopupClickListener {
        void onItemClick();
    }

    private View popupView;
    private ImageView mIv_clear;
    private TextView mTv_cash;
    private TextView mTv_info;
    private PayPsdInputView mPp_v;
    private TextView mTv_shop_payment_1;
    private TextView mTv_shop_payment_2;
    private TextView mTv_shop_payment_3;
    private TextView mTv_shop_payment_4;
    private TextView mTv_shop_payment_5;
    private TextView mTv_shop_payment_6;
    private TextView mTv_shop_payment_7;
    private TextView mTv_shop_payment_8;
    private TextView mTv_shop_payment_9;
    private TextView mTv_shop_payment_0;
    private ImageView mIv_shop_payment_down;
    private ImageView mIv_shop_payment_dot;
    private ImageView mIv_shop_payment_del;
    private RelativeLayout mRl_send;
    private String mPassword;
    private TextView mTv_forget;
    private String mAccountName;

    public PasswordPayPopup(Activity context) {
        super(context);
        bindEvent();
    }

    @Override
    public Animation getAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_password_pay_popup, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            initPassView();
            initKeBoardView();
            initEvent();
//            initData();
        }
    }

    private void initEvent() {
        mIv_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        mTv_shop_payment_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "1");
                } else {
                    mPp_v.setText("1");
                }

            }
        });
        mTv_shop_payment_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "2");
                } else {
                    mPp_v.setText("2");
                }

            }
        });
        mTv_shop_payment_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "3");
                } else {
                    mPp_v.setText("3");
                }

            }
        });
        mTv_shop_payment_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "4");
                } else {
                    mPp_v.setText("4");
                }

            }
        });
        mTv_shop_payment_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "5");
                } else {
                    mPp_v.setText("5");
                }
            }
        });
        mTv_shop_payment_6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "6");
                } else {
                    mPp_v.setText("6");
                }

            }
        });
        mTv_shop_payment_7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "7");
                } else {
                    mPp_v.setText("7");
                }

            }
        });
        mTv_shop_payment_8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "8");
                } else {
                    mPp_v.setText("8");
                }

            }
        });
        mTv_shop_payment_9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "9");
                } else {
                    mPp_v.setText("9");
                }

            }
        });
        mTv_shop_payment_0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + "0");
                } else {
                    mPp_v.setText("0");
                }

            }
        });
        mIv_shop_payment_dot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (mPassword.length() >= 6) {
                    ShowToast.showShortToast("请确认支付");
                    return;
                }
                if (!TextUtils.isEmpty(mPassword)) {
                    mPp_v.setText(mPassword + ".");
                } else {
                    mPp_v.setText(".");
                }
            }
        });

        mIv_shop_payment_down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mIv_shop_payment_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPassword = mPp_v.getText().toString().trim();
                if (!TextUtils.isEmpty(mPassword)) {
                    String substringCash = mPassword.substring(0, mPassword.toString().length() - 1);
                    mPp_v.setText(substringCash);
                }
            }
        });

        mRl_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickSurePayListener.onClickItem(mTv_sure, mProgress_wheel, mPp_v.getText().toString().trim());
            }
        });
        mTv_forget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemClickListener.onItemClick();
            }
        });
    }

    private void initPassView() {
        mIv_clear = (ImageView) popupView.findViewById(R.id.iv_clear);
        mTv_cash = (TextView) popupView.findViewById(R.id.tv_cash);
        mTv_info = (TextView) popupView.findViewById(R.id.tv_info);
        mPp_v = (PayPsdInputView) popupView.findViewById(R.id.pp_v);

        mTv_forget = (TextView) popupView.findViewById(R.id.tv_forget);
    }

    private void initKeBoardView() {
        mTv_shop_payment_1 = (TextView) popupView.findViewById(R.id.tv_shop_payment_1);
        mTv_shop_payment_2 = (TextView) popupView.findViewById(R.id.tv_shop_payment_2);
        mTv_shop_payment_3 = (TextView) popupView.findViewById(R.id.tv_shop_payment_3);
        mTv_shop_payment_4 = (TextView) popupView.findViewById(R.id.tv_shop_payment_4);
        mTv_shop_payment_5 = (TextView) popupView.findViewById(R.id.tv_shop_payment_5);
        mTv_shop_payment_6 = (TextView) popupView.findViewById(R.id.tv_shop_payment_6);
        mTv_shop_payment_7 = (TextView) popupView.findViewById(R.id.tv_shop_payment_7);
        mTv_shop_payment_8 = (TextView) popupView.findViewById(R.id.tv_shop_payment_8);
        mTv_shop_payment_9 = (TextView) popupView.findViewById(R.id.tv_shop_payment_9);
        mTv_shop_payment_0 = (TextView) popupView.findViewById(R.id.tv_shop_payment_0);
        mTv_sure = (TextView) popupView.findViewById(R.id.tv_sure);
        mIv_shop_payment_down = (ImageView) popupView.findViewById(R.id.iv_shop_payment_down);
        mIv_shop_payment_dot = (ImageView) popupView.findViewById(R.id.iv_shop_payment_dot);
        mIv_shop_payment_del = (ImageView) popupView.findViewById(R.id.iv_shop_payment_del);
        mProgress_wheel = (ProgressWheel) popupView.findViewById(R.id.progress_wheel);
        LinearLayout ll_shop_payment_keyboard = (LinearLayout) popupView.findViewById(R.id.ll_shop_payment_keyboard);
        mRl_send = (RelativeLayout) popupView.findViewById(R.id.rl_send);
        ll_shop_payment_keyboard.setAnimation(getAnimation());
    }

    public void setAccountName(String accountName) {
        if (null != mTv_info && !TextUtils.isEmpty(accountName)) {
            mTv_info.setVisibility(View.VISIBLE);
            mTv_info.setText(accountName);
        }
    }

    public void setBalance(String balance) {
        if (null != mTv_cash && !TextUtils.isEmpty(balance)) {
            mTv_cash.setVisibility(View.VISIBLE);
            mTv_cash.setText(balance);
        }
    }

    public interface OnClickItemListener {
        void onClickItem(String payPlatform, View v);
    }

    public interface OnClickSurePayListener {
        void onClickItem(TextView mTv_sure, ProgressWheel mProgress_wheel, String mPassword);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        OnClickItemListener onClicktemListener = onClickItemListener;
    }

    public void setTextGoneWheelVisiable() {
        mProgress_wheel.setVisibility(View.VISIBLE);
        mTv_sure.setVisibility(View.GONE);
    }

    public void setTextVisiableWheelGone() {
        mProgress_wheel.setVisibility(View.GONE);
        mTv_sure.setVisibility(View.VISIBLE);
    }

    public void setTextClear() {
        mPp_v.setText("");
    }
}
